//
//  FilterView.swift
//  ShopIOS
//
//  Created by Apple on 2019/8/7.
//  Copyright © 2019 Apple. All rights reserved.
//

import UIKit

class FilterView: UIView {
    var brand=[mFilterBrand]()
    var category=[mFilterCategory]()
    var attrs=[mFilterAttrs]()
    @IBOutlet weak var btn_confirm: UIButton!
    @IBOutlet weak var btn_reset: UIButton!
    @IBOutlet weak var collectionview: UICollectionView!
    /// 点击回调
    var block: ((String,String,String)->Void)?
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    func loadData(keyword:String,cid:String,a_id:String,b_id:String){
        var d = ["keyword":keyword,
//                 "cid":cid,
//                 "a_id":a_id,
//                 "b_id":b_id,
                 "userkey":userkey,
                 "app_key":app_key,
                 "timestamp":getTimestamp()
            ] as! [String : String]
        if cid != ""{
            d["cid"]=cid
        }
        if a_id != ""{
            d["a_id"]=a_id
        }
        if b_id != ""{
            d["b_id"]=b_id
        }
        let sign=SignTopRequest(params: d)
        d["sign"]=sign
        AlamofireHelper.get(url: GetSearchFilter, parameters: d, successHandler: {[weak self](res)in
            HUD.dismiss()
            guard let ss = self else {return}
            let result=mFilterResult.init(json: res)
            ss.brand=result.Brand
            ss.category=result.Category
            ss.attrs=result.Attrs
            ss.collectionview.reloadData()
        }){[weak self] (error) in
            HUD.dismiss()
            guard let ss = self else {return}
        }
    }
}
extension FilterView:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        switch section {
        case 0:
            return brand.count
        case 1:
            return category.count
        default:
            return attrs[section-2].AttrValues.count
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell=collectionView.dequeueReusableCell(withReuseIdentifier: "re", for: indexPath) as!FilterCell
        cell.lb_cell.backgroundColor = "#eeeeee".color()
        cell.lb_cell.layer.cornerRadius=2
        cell.lb_cell.clipsToBounds=true
        switch indexPath.section {
        case 0:
            let item=brand[indexPath.row]
            cell.lb_cell.text=item.Name
            if item.isSelected {
                cell.lb_cell.backgroundColor = "#F8CDC2".color()
                cell.lb_cell.border(color: UIColor.red, width: 1, type: UIBorderSideType.UIBorderSideTypeAll, cornerRadius: 2)
            }else{
                cell.lb_cell.backgroundColor = "#eeeeee".color()
                cell.lb_cell.border(color: "#eeeeee".color(), width: 1, type: UIBorderSideType.UIBorderSideTypeAll, cornerRadius: 2)
            }
        case 1:
            let item=category[indexPath.row]
            cell.lb_cell.text=item.Name
            if item.isSelected {
                cell.lb_cell.backgroundColor = "#F8CDC2".color()
                cell.lb_cell.border(color: UIColor.red, width: 1, type: UIBorderSideType.UIBorderSideTypeAll, cornerRadius: 2)
            }else{
                cell.lb_cell.backgroundColor = "#eeeeee".color()
                cell.lb_cell.border(color: "#eeeeee".color(), width: 1, type: UIBorderSideType.UIBorderSideTypeAll, cornerRadius: 2)
            }
        default:
            let item=attrs[indexPath.section-2].AttrValues[indexPath.row]
            cell.lb_cell.text=item.Name
            if item.isSelected {
                cell.lb_cell.backgroundColor = "#F8CDC2".color()
                cell.lb_cell.border(color: UIColor.red, width: 1, type: UIBorderSideType.UIBorderSideTypeAll, cornerRadius: 2)
            }else{
                cell.lb_cell.backgroundColor = "#eeeeee".color()
                cell.lb_cell.border(color: "#eeeeee".color(), width: 1, type: UIBorderSideType.UIBorderSideTypeAll, cornerRadius: 2)
            }
        }
        return cell
        
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return attrs.count+2
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var reusableview:FilterHeadView!
        
        //分区头
        if kind == UICollectionView.elementKindSectionHeader{
            reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
                                                                           withReuseIdentifier: "head", for: indexPath) as!FilterHeadView
            //设置头部标题
            switch indexPath.section {
            case 0:
                reusableview.lb_title.text="品牌"
            case 1:
                reusableview.lb_title.text="分类"
            default:
                reusableview.lb_title.text=attrs[indexPath.section-2].Name
            }
            
        }
        return reusableview
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.size.width, height: 40)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let padding: CGFloat =  5
        let collectionViewSize = collectionView.frame.size.width - 4*padding
        
        return CGSize(width: collectionViewSize/3, height: 40)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch indexPath.section {
        case 0:
            let item=brand[indexPath.row]
            item.isSelected = !item.isSelected
        case 1:
            let item=category[indexPath.row]
            item.isSelected = !item.isSelected
        default:
            let item=attrs[indexPath.section-2].AttrValues[indexPath.row]
            item.isSelected = !item.isSelected
        }
        var b_id=""
        var c_id=""
        var s_id=""
        for brand in brand{
            if brand.isSelected {
                b_id+=String(brand.Id)+","
            }
        }
        for cate in category{
            if cate.isSelected {
                c_id+=String(cate.Id)+","
            }
        }
        for attr in attrs{
            for s in attr.AttrValues{
                if s.isSelected {
                    s_id+=String(s.Id)+","
                }
            }
        }
        if b_id.count>0{
            b_id=b_id.prefix(b_id.count-1).description
        }
        if c_id.count>0{
            c_id=c_id.prefix(c_id.count-1).description
        }
        if s_id.count>0{
            s_id=s_id.prefix(s_id.count-1).description
        }
        self.block?(b_id,c_id,s_id)
        collectionView.reloadData()
    }
}
